package admin.models

import LoadingStatus
import models.LocationUi
import my.models.ComponentUi
import my.models.ComputerUi
import my.models.MyObject

data class AdminObjectsViewState(
    val components: List<ComponentUi> = emptyList(),
    val computers: List<ComputerUi> = emptyList(),
    val locations: List<LocationUi> = emptyList(),
    val loadingStatus: LoadingStatus = LoadingStatus.Idle,
    val selected: MyObject? = null,
)

interface AdminObject {
    val name: String
    val serialNumber: String
    val locationName: String
    val locationId: Int
}

data class CreateComponent(
    override val name: String = "",
    override val serialNumber: String = "",
    override val locationName: String = "",
    override val locationId: Int = -1,
    val type: String = "",
    val specifications: String = "",
    val computerId: Int? = null,
    val purchaseDate: Long = 0L
) : AdminObject {
    companion object {
        fun fromUi(ui: ComponentUi): CreateComponent =
            CreateComponent(
                name = ui.name,
                serialNumber = ui.serialNumber,
                type = ui.type,
                specifications = ui.specifications,
                computerId = ui.computerId,
                purchaseDate = ui.purchaseDate.toLong(),
                locationId = ui.location.id
            )
    }
}

data class CreateComputer(
    override val serialNumber: String = "",
    override val locationName: String = "",
    override val locationId: Int = -1,
    val model: String = "",
    val manufacturer: String = "",
    val purchaseDate: Long = -1,
    val notes: String? = null
) : AdminObject {
    override val name: String
        get() = "$manufacturer $model"

    companion object {
        fun fromUi(ui: ComputerUi): CreateComputer =
            CreateComputer(
                serialNumber = ui.serialNumber,
                model = ui.model,
                manufacturer = ui.manufacturer,
                purchaseDate = ui.purchaseDate.toLong(),
                locationId = ui.location.id,
                notes = ui.notes
            )
    }
}